<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <!-- logo -->
      <div class="logo"></div>
      <!-- 菜单 -->
      <el-menu
        background-color="#023"
        style="border-right: none"
        text-color="#fff"
        :default-active="$route.path"
        router
      >
        <el-menu-item index="/">
          <i class="el-icon-s-home"></i>
          <span slot="title">数据概览</span>
        </el-menu-item>
        <el-menu-item index="/article">
          <i class="el-icon-document"></i>
          <span slot="title">内容管理</span>
        </el-menu-item>
        <el-menu-item index="/publish">
          <i class="el-icon-s-promotion"></i>
          <span slot="title">发布文章</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <span style="margin-right: 20px">
          {{ userInfo?.name || userInfo?.mobile }}
        </span>
        <el-link icon="el-icon-unlock" :underline="false" @click="logout">
          退出
        </el-link>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getProfile } from '@/api/user'
import { createNamespacedHelpers } from 'vuex'
const { mapMutations } = createNamespacedHelpers('user')

export default {
  name: 'LayoutPage',
  // props: {},
  // components: {},
  data() {
    return {
      userInfo: null,
    }
  },
  // computed: {},
  // watch: {},
  // 钩子函数
  created() {
    this._getProfile()
  },
  methods: {
    async _getProfile() {
      const [err, res] = await getProfile()
      // console.log('获取用户信息', err, res)
      if (err) return this.$message.error(err.response.data.message)
      const { data } = res
      this.userInfo = data
    },
    ...mapMutations(['removeToken']),
    // 登出
    logout() {
      this.removeToken()
      this.$router.push({ name: 'login' })
    },
  },
}
</script>

<style scoped lang="scss">
.layout-container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  .el-aside {
    background: #023;
    .logo {
      width: 200px;
      height: 60px;
      background: #023;
      // background: #023 url('@/assets/images/logo_admin.png') no-repeat center /
      //   160px auto;
    }
  }
  .el-header {
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>
